// pages/farmer/feedRecord/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
formatTime = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordList:[],
    pageIndex:1,
    pageSize:10,
    currentTab:0,
    tabType:[0,2],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.onHttpInitFeedRecord(0);
  },
  handleChange({ detail }) {
    console.log('detail',detail)
    this.data.pageIndex = 1;
    this.data.recordList = [];
    this.data.currentTab = detail.index;
    this.setData({
      recordList : [],
      currentTab: detail.index
    })
    this.onHttpInitFeedRecord(detail.index);
  },
  /**
   *  获取附近的人的用户列表
   */
  onHttpInitFeedRecord(tabIndex) {
    console.log('that.data.tabType[tabIndex]',this.data.tabType[tabIndex])
    var that = this;
    fetchApi.httpPost('/farm/feed/record/list', {
      record_type:that.data.tabType[tabIndex],
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize,
    }, function (res) {
      console.log("获取农场足迹记录信息", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1;
        var recordList = res.data.data;
        if(recordList.length > 0){
          for (var index in recordList) {
            that.data.recordList.splice(that.data.recordList.length, 0, recordList[index]);
          }
          that.setData({
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }else{
        // Toast.fail(res.data.msg)
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      recordList : [],
      loading: false,
      netWork: false,
      isLoading: true
    })
    var type = this.data.tabType[this.data.currentTab]
    this.onHttpInitFeedRecord(type);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var type = this.data.tabType[this.data.currentTab]
    this.onHttpInitFeedRecord(type);
  },
})